import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { Octree } from 'three/examples/jsm/math/Octree.js';
import { OctreeHelper } from 'three/examples/jsm/helpers/OctreeHelper.js';
// 引入/examples/jsm/math/目录下胶囊扩展库Capsule.js
import { Capsule } from 'three/examples/jsm/math/Capsule.js';
import CapsuleHelper from './capsuleHelper.js'


const model = new THREE.Group();
const loader = new GLTFLoader()
const gltf = await loader.loadAsync('/士兵.glb')
const player = gltf.scene
// player.position.x = 50
player.rotation.y = Math.PI
model.add(player)
const axesHelper = new THREE.AxesHelper(5);
player.add(axesHelper)
let mixer = null
//包含关键帧动画的模型对象作为AnimationMixer的参数创建一个播放器mixer
mixer = new THREE.AnimationMixer(model);
//AnimationMixer的`.clipAction()`返回一个AnimationAction对象
console.log('',gltf.animations);
const clipActionIdle = mixer.clipAction(gltf.animations[0]); 
const clipActionWalk = mixer.clipAction(gltf.animations[3]); 
const clipActionRun = mixer.clipAction(gltf.animations[1]); 
clipActionIdle.play(); 
clipActionWalk.play(); 
clipActionRun.play(); 
clipActionIdle.weight = 1.0
clipActionWalk.weight = 0.0 
clipActionRun.weight = 0.0
const clock = new THREE.Clock();

function loop() {
    requestAnimationFrame(loop);
    //clock.getDelta()方法获得loop()两次执行时间间隔
    const frameT = clock.getDelta()
    mixer.update(frameT)
}
loop();
const gltf2 = await loader.loadAsync('/工厂.glb')
console.log('',gltf2.scene);
model.add(gltf2.scene);
const nameNode = gltf2.scene.getObjectByName("仓库");
const worldOctree = new Octree();
worldOctree.fromGraphNode(nameNode);
const helper = new OctreeHelper( worldOctree );
model.add( helper );

const R = 0.4;//胶囊半径
const H = 1.7;//胶囊总高度
const start = new THREE.Vector3(0, R, 0);//底部半球球心坐标
const end = new THREE.Vector3(0, H - R, 0);//顶部半球球心坐标
const capsule = new Capsule(start, end, R);
// capsule.translate(new THREE.Vector3(0, R, 0));
// const result = worldOctree.capsuleIntersect(capsule);
// 可视化胶囊几何体
const capsuleHelper = CapsuleHelper(R, H);
model.add(capsuleHelper);
capsuleHelper.position.x += 25.5;
capsule.translate(new THREE.Vector3(25.5, 0, 0));
const result = worldOctree.capsuleIntersect(capsule);
console.log('碰撞检测结果',result);


export default {model,player,mixer,clipActionIdle,clipActionWalk,clipActionRun,capsule,worldOctree,capsuleHelper};